package com.wanlian.router.ui.manage;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.smart.android.ui.tools.MyNiuBAdapter;
import com.smart.android.utils.DateTime;
import com.wanlian.router.R;
import com.wanlian.router.bean.Blacklist;
import com.wanlian.router.net.NetWorkingClient;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/2.
 * Email: fvaryu@qq.com
 */
public class BlacklistAdapter extends MyNiuBAdapter<Blacklist> {

    private View.OnClickListener mOnClickListener;

    public void setOnClickListener(View.OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public BlacklistAdapter(Context context, List<Blacklist> mTs) {
        super(context, mTs);
    }

    @Override
    public int layout() {
        return R.layout.layout_item_blacklist;
    }

    @Override
    public MyViewHolder<Blacklist> holder(View view) {
        return new BlacklistHolder(view);
    }

    class BlacklistHolder extends MyViewHolder<Blacklist> {

        @BindView(R.id.image_logo)
        ImageView mLogo;
        @BindView(R.id.tv_name)
        TextView mName;
        @BindView(R.id.tv_mac)
        TextView mMac;
        @BindView(R.id.tv_time)
        TextView mTime;
        @BindView(R.id.btnDelete)
        Button btnDelete;

        public BlacklistHolder(View view) {
            super(view);
        }

        @Override
        public void setData(int i, Blacklist blacklist) {

            mName.setText(blacklist.getMacId());
            mMac.setText(blacklist.getMacAddress());

            DateTime now = new DateTime(System.currentTimeMillis());
            DateTime dateTime = new DateTime(blacklist.getConnetTime());
            int monthC = now.getMonth() - dateTime.getMonth();
            int dayC = now.getDay() - dateTime.getDay();
            int minuteC = now.getMinute() - dateTime.getMinute();
            int hourC = now.getHour() - dateTime.getHour();
            if (monthC > 1) {
                mTime.setText(dateTime.getStringDates(DateTime.FORMAT_COMMON));
            } else if (monthC == 1) {
                mTime.setText(String.format(Locale.getDefault(), "%d月前", monthC));
            } else if (dayC > 1) {
                mTime.setText(String.format(Locale.getDefault(), "%d天前", dayC));
            } else if (hourC >= 1) {
                mTime.setText(String.format(Locale.getDefault(), "%d小时前", hourC));
            } else if (minuteC >= 1) {
                mTime.setText(String.format(Locale.getDefault(), "%d分钟前", minuteC));
            } else {
                mTime.setText("刚刚");
            }

            btnDelete.setTag(blacklist);
        }

        @OnClick(R.id.btnDelete)
        void delete(View view) {

            if (mOnClickListener != null) {
                mOnClickListener.onClick(view);
            }
        }
    }
}
